package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "用户角色")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    //1 用户条件分页查询接口
    @Operation(summary = "角色列表分页查询--方法一")
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable("pageNum") Integer pageNum,
                             @PathVariable("pageSize") Integer pageSize,
                             SysRoleDto sysRoleDto) {
        PageInfo<SysRole> pageInfo = sysRoleService.findByPage(pageNum,pageSize,sysRoleDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "角色列表分页查询--方法二")
    @GetMapping(value = "/findByPage2/{pageNum}/{pageSize}")
    public Result findByPage2(@PathVariable("pageNum") Integer pageNum,
                              @PathVariable("pageSize") Integer pageSize,
                              SysRoleDto sysRoleDto) {
        Page<SysRole> pageParam = new Page<>(pageNum, pageSize);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, sysRoleDto);
        return Result.build(pageModel, ResultCodeEnum.SUCCESS);

    }

    @Operation(summary = "添加角色-方法一")
    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加角色-方法二")
    @PostMapping("/saveSysRole2")
    public Result saveSysRole2(@RequestBody SysRole sysRole) {
        sysRoleService.saveSysRole2(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改角色-方法一")
    @PostMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole) {
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改角色-方法二")
    @PostMapping("/updateSysRole2")
    public Result updateSysRole2(@RequestBody SysRole sysRole) {
        sysRoleService.updateById(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //4 角色删除的方法
    @PostMapping("/deleteById/{roleId}")
    public Result deleteById(@PathVariable("roleId") Long roleId) {
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //4 角色删除的方法
    @PostMapping("/deleteById2/{roleId}")
    public Result deleteById2(@PathVariable("roleId") Long roleId) {
        sysRoleService.removeById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //查询所有角色
    @Operation(summary = "查询角色")
    @GetMapping("/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable("userId") Long userId) {
        Map<String,Object> map = sysRoleService.findAll(userId);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }

}
